<template>
  <div class="table-container">
    <vab-query-form>
      <!--      <vab-query-form-right-panel>-->
      <el-form
        ref="form"
        :model="helpForm"
        :inline="true"
        @submit.native.prevent
      >
        <el-form-item label="互助标题">
          <el-input v-model="helpForm.helpTitle" clearable placeholder="标题" />
        </el-form-item>

        <el-form-item label="互助类别">
          <el-select
            v-model="helpForm.helpClassification"
            clearable
            placeholder="请选择"
          >
            <el-option
              v-for="item in classificationList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="有效期限">
          <el-date-picker
            v-model="helpForm.endTime"
            type="date"
            placeholder="选择日期"
            value-format="yyyy-MM-dd"
            style="width: 200px"
          ></el-date-picker>
        </el-form-item>

        <el-form-item label="有效区域">
          <el-input
            v-model="helpForm.effectiveArea"
            clearable
            placeholder="有效区域"
          />
        </el-form-item>

        <el-form-item label="更新日期">
          <el-date-picker
            v-model="helpForm.updateTime"
            type="date"
            placeholder="选择日期"
            value-format="yyyy-MM-dd"
            style="width: 200px"
          ></el-date-picker>
        </el-form-item>

        <el-form-item label="是否置顶">
          <el-select v-model="helpForm.postTop" clearable placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="审核状态">
          <el-select v-model="helpForm.examine" clearable placeholder="请选择">
            <el-option
              v-for="item in status"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button
            icon="el-icon-search"
            type="primary"
            native-type="submit"
            @click="handleQuery"
          >
            查询
          </el-button>
        </el-form-item>
      </el-form>
      <vab-query-form-left-panel>
        <el-button icon="el-icon-plus" type="primary" @click="handleAdd">
          添加
        </el-button>
        <el-button icon="el-icon-delete" type="danger" @click="handleDelete">
          批量删除
        </el-button>
      </vab-query-form-left-panel>
      <!--      </vab-query-form-right-panel>-->
    </vab-query-form>

    <el-table
      ref="tableSort"
      v-loading="listLoading"
      :data="list"
      :element-loading-text="elementLoadingText"
      :height="height"
      @selection-change="setSelectRows"
      @sort-change="tableSortChange"
    >
      <el-table-column
        show-overflow-tooltip
        type="selection"
        width="55"
        header-align="center"
        align="center"
      ></el-table-column>
      <el-table-column
        show-overflow-tooltip
        label="序号"
        width="95"
        header-align="center"
        align="center"
      >
        <template #default="scope">
          {{ scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column
        show-overflow-tooltip
        prop="helpTitle"
        label="标题"
        header-align="center"
        align="center"
      ></el-table-column>
      <el-table-column
        show-overflow-tooltip
        prop="classificationName"
        label="类别"
        header-align="center"
        align="center"
      ></el-table-column>
      <el-table-column
        show-overflow-tooltip
        prop="endTime"
        label="有效期限"
        header-align="center"
        align="center"
      ></el-table-column>
      <el-table-column
        show-overflow-tooltip
        prop="effectiveArea"
        label="有效区域"
        header-align="center"
        align="center"
      ></el-table-column>
      <el-table-column
        show-overflow-tooltip
        prop="helpPictureId"
        label="图片ID"
        header-align="center"
        align="center"
      ></el-table-column>
      <el-table-column
        show-overflow-tooltip
        prop="updateTime"
        label="更新时间"
        header-align="center"
        align="center"
      ></el-table-column>
      <el-table-column
        show-overflow-tooltip
        prop="examine"
        label="审核状态"
        header-align="center"
        align="center"
      >
        <template #default="{ row }">
          <el-tag v-if="row.examine === 0" type="" @click="examine(row)">
            待审核
          </el-tag>
          <el-tag v-if="row.examine === 1" type="success" @click="examine(row)">
            已通过
          </el-tag>
          <el-tag v-if="row.examine === 2" type="danger" @click="examine(row)">
            未通过
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column
        show-overflow-tooltip
        prop="postTop"
        label="置顶"
        header-align="center"
        align="center"
      >
        <template #default="{ row }">
          <el-switch
            v-model="row.postTop"
            :active-value="1"
            :inactive-value="0"
            @change="postTopChange(row)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column
        show-overflow-tooltip
        label="操作"
        width="180px"
        header-align="center"
        align="center"
      >
        <template #default="{ row }">
          <el-button type="text" @click="handleEdit(row)">编辑</el-button>
          <el-button type="text" @click="handleDelete(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :background="background"
      :current-page="helpForm.page"
      :layout="layout"
      :page-size="helpForm.size"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    ></el-pagination>
    <examine ref="examine" @fetchData="fetchData"></examine>
  </div>
</template>

<script>
  import {
    helpSelectBy,
    helpPostTop,
    classificationSelectView,
    helpDeleteByIds,
  } from '@/api/management/management'
  import Examine from '../../components/Edit/Examine'
  export default {
    name: 'ActivityManagement',
    components: {
      Examine,
    },
    filters: {
      // statusFilter(status) {
      //   const statusMap = {
      //     就读: '就读',
      //     教职工: '教职工',
      //   }
      //   return statusMap[status]
      // },
    },
    data() {
      return {
        imgShow: true,
        list: [],
        imageList: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        background: true,
        selectRows: '',
        elementLoadingText: '正在加载...',
        helpForm: {
          page: 1,
          size: 20,
          helpTitle: '',
          helpClassification: '',
          endTime: '',
          effectiveArea: '',
          detailedDescription: '',
          helpPictureId: '',
          supportNumber: '',
          examine: '',
          postTop: '',
        },
        options: [
          {
            value: '1',
            label: '是',
          },
          {
            value: '0',
            label: '否',
          },
        ],
        status: [
          {
            value: '0',
            label: '审核中',
          },
          {
            value: '1',
            label: '已通过',
          },
          {
            value: '2',
            label: '未通过',
          },
        ],
        classificationList: {},
      }
    },
    computed: {
      height() {
        return this.$baseTableHeight()
      },
    },
    created() {
      this.getClassificationList()
      this.fetchData()
    },
    beforeDestroy() {},
    mounted() {},
    methods: {
      tableSortChange() {
        const imageList = []
        debugger
        this.$refs.tableSort.tableData.forEach((item, index) => {
          imageList.push(item.img)
        })
        this.imageList = imageList
      },
      setSelectRows(val) {
        this.selectRows = val
      },
      handleAdd() {
        // this.$refs['edit'].showEdit()
        this.$router.push({
          path: '/editHelp',
          query: '',
        })
      },
      handleEdit(row) {
        // this.$refs['edit'].showEdit(row)
        this.$router.push({
          path: '/editHelp',
          query: {
            row: row,
          },
        })
      },
      handleDelete(row) {
        if (row.id) {
          this.$baseConfirm('你确定要删除当前项吗', null, async () => {
            await helpDeleteByIds([row.id]).then((res) => {
              if (res.code == 200) {
                this.$baseMessage(res.data, 'success')
                this.fetchData()
              } else {
                this.$baseMessage('删除失败', 'error')
              }
            })
          })
        } else {
          if (this.selectRows.length > 0) {
            const ids = this.selectRows.map((item) => item.id)
            this.$baseConfirm('你确定要删除选中项吗', null, async () => {
              await helpDeleteByIds(ids).then((res) => {
                if (res.code == 200) {
                  this.$baseMessage(res.data, 'success')
                  this.fetchData()
                } else {
                  this.$baseMessage('删除失败', 'error')
                }
              })
            })
          } else {
            this.$baseMessage('未选中任何行', 'error')
            return false
          }
        }
      },
      //获取类别列表
      async getClassificationList() {
        await classificationSelectView({ index: 0 }).then((res) => {
          if (res.code == 200) {
            this.classificationList = res.data
          } else {
            this.$baseMessage('类别列表获取失败', 'error')
          }
        })
      },
      //置顶
      async postTopChange(row) {
        await helpPostTop(row).then((res) => {
          if (res.code !== 200) {
            this.$baseMessage('置顶失败', 'error')
          } else {
            this.fetchData()
          }
        })
      },
      //审核
      examine(row) {
        this.$refs['examine'].showEdit(row, 0)
      },
      handleSizeChange(val) {
        this.helpForm.size = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.helpForm.page = val
        this.fetchData()
      },
      handleQuery() {
        this.helpForm.page = 1
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        //数据隔离
        this.helpForm.root = 1
        await helpSelectBy(this.helpForm).then((res) => {
          if (res.code === 200) {
            this.list = res.data
            const imageList = []
            //总数，决定页数
            this.total = 50
          } else {
            this.$baseMessage('数据获取失败', 'error')
          }
        })
        setTimeout(() => {
          this.listLoading = false
        }, 500)
      },
    },
  }
</script>
